﻿/**
 * Peter
 * 
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 *    
 *  This code is provided on an AS IS basis, with no WARRANTIES,
 *  CONDITIONS or GUARANTEES of any kind.
 *  
 **/
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Peter.Logic;
using System.Linq;

namespace Peter.Tests.Logic
{
   /// <summary>
   /// Tests the main view model.
   /// </summary>
   [TestClass]
   public class ModelPeterMainTests
   {
      private const string TEST_FILE = "TestFile.txt";
      private const string CSHARP_TEST_FILE = "CSTestFile.cs";
      private const string NON_EXISTING_FILE = "NonExistingTestFile.txt";

      /// <summary>
      /// testing the count and content of editor collection after opening single file 
      /// </summary>
      [TestMethod]
      public void OpenFileTest ()
      {
         var target = new ModelPeterMain ();

         target.Initialize ();
         target.Open (NON_EXISTING_FILE);
         var editorCollection = target.Documents.OfType<ModelEditor> ().ToList ();
         Assert.AreEqual (1, editorCollection.Count);
         var openedFile = editorCollection[0];
         Assert.AreEqual (NON_EXISTING_FILE, openedFile.FileInfo.Name);
      }

      /// <summary>
      /// Opening the same file twice
      /// </summary>
      [TestMethod]
      public void OpenTheSameFileTwice ()
      {
         var target = new ModelPeterMain ();

         target.Initialize ();
         target.Open (NON_EXISTING_FILE);
         target.Open (NON_EXISTING_FILE);
         var editorCollection = target.Documents.OfType<ModelEditor> ().ToList ();
         Assert.AreEqual (1, editorCollection.Count);
         var openedFile = editorCollection[0];
         Assert.AreEqual (NON_EXISTING_FILE, openedFile.FileInfo.Name);
      }

      /// <summary>
      /// Opening file, title should be updated with the file name
      /// </summary>
      [TestMethod]
      public void TitleAfterOpeningFile ()
      {
         var target = new ModelPeterMain ();
         target.Initialize ();
         target.Open (TEST_FILE);
         var editorCollection = target.Documents.OfType<ModelEditor> ().ToList ();
         var openedFile = editorCollection[0];
         target.ActiveContent = openedFile;
         Assert.AreEqual ("Peter - TestFile.txt", target.Title);
      }

      /// <summary>
      /// Title is updated after closing a file
      /// </summary>
      [TestMethod]
      public void TitleUpdatedAfterClosingFile ()
      {
         var target = new ModelPeterMain ();
         target.Initialize ();
         target.Open (TEST_FILE);
         var openedFile = (ModelEditor)target.ActiveContent;
         openedFile.CloseCommand.Execute (null);
         Assert.AreEqual ("Peter", target.Title);
      }

      /// <summary>
      /// Title and document count after creating new file
      /// </summary>
      [TestMethod]
      public void NewFileTest ()
      {
         var target = new ModelPeterMain ();
         target.Initialize ();
         target.NewFile ();
         Assert.AreEqual (target.Documents.Count, 1);
         Assert.AreEqual ("Peter - New Document", target.Title);
      }

      /// <summary>
      /// Opening 2 files and checking the count and title
      /// </summary>
      [TestMethod]
      public void TitleAfterOpeningTwoFiles ()
      {
         var target = new ModelPeterMain ();
         target.Initialize ();
         target.Open (TEST_FILE);
         target.Open (CSHARP_TEST_FILE);
         Assert.AreEqual (2, target.Documents.Count, "Count Failed");
         Assert.AreEqual ("Peter - CSTestFile.cs", target.Title, "Title Failed");
      }

      /// <summary>
      /// Opening 2 files, closing 1 then checking the count and title
      /// </summary>
      [TestMethod]
      public void TitleAfterOpeningTwoFilesAndClosingOne ()
      {
         var target = new ModelPeterMain ();
         target.Initialize ();
         target.Open (TEST_FILE);
         target.Open (CSHARP_TEST_FILE);
         ((ModelEditor)target.ActiveContent).CloseCommand.Execute (null);
         Assert.AreEqual (1, target.Documents.Count, "Count Failed");
         Assert.AreEqual ("Peter - TestFile.txt", target.Title, "Title Failed");
      }

      /// <summary>
      /// Showing option should change the title
      /// </summary>
      [TestMethod]
      public void ShowOptionsTitleTest ()
      {
         var target = new ModelPeterMain ();
         target.Initialize ();
         target.ShowOptions ();
         Assert.AreEqual ("Peter - Options", target.Title, "Title Failed");
      }

      /// <summary>
      /// Showing options should display the options tab as the active editable
      /// </summary>
      [TestMethod]
      public void ShowOptionsActiveEditableTest ()
      {
         var target = new ModelPeterMain ();
         target.Initialize ();
         target.ShowOptions ();
         var modelOptions = (ModelEditor)target.ActiveEditable;
         Assert.AreEqual (typeof (ModelOptions), modelOptions.GetType (), "Options tab is not the current Active Editable");
      }
   }
}
